import 'package:flutter/material.dart';
import 'package:slwl/pages/home/index.dart';
import 'package:slwl/pages/message/index.dart';
import 'package:slwl/pages/mine/index.dart';
import 'package:slwl/utils/token.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  int _currentIndex = 0;

  List tabBarList = [
    {
      "label": "首页",
      "default": "assets/tabbar/home_default.png",
      "active": "assets/tabbar/home_active.png",
    },
    {
      "label": "消息",
      "default": "assets/tabbar/message_default.png",
      "active": "assets/tabbar/message_active.png",
    },
    {
      "label": "我的",
      "default": "assets/tabbar/mine_default.png",
      "active": "assets/tabbar/mine_active.png",
    },
  ];

  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> items = [];
    for (var item in tabBarList) {
      items.add(
        BottomNavigationBarItem(
          label: item["label"],
          icon: Image.asset(
            item["default"],
            width: 22,
            height: 22,
          ),
          activeIcon: Image.asset(
            item["active"],
            width: 22,
            height: 22,
          ),
        ),
      );
    }
    return items;
  }

  @override
  void initState() {
    super.initState();
    // 初始化首页
    _currentIndex = 0;
    //初始化token
    TokenManager tokenManager = TokenManager();
    Future.delayed(Duration.zero, () {
      tokenManager.init();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: _currentIndex,
          children: [
            HomePage(),
            MessagePage(),
            MinePage(),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: barItemList(),
        currentIndex: _currentIndex,
        //#818181
        unselectedItemColor: Color(0xFF818181),
        //#EF4F3F
        selectedItemColor: Color(0xFFEF4F3F),
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}
